<?
include_once('conn.php');
include_once('include/func.php');
include("user_check.php") ;

///
if(isset($_GET[perpage])){
	setcookie("perpage", $_GET[perpage]);
	echo $current_url = 'HTTP://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	header("Location: $nowpage");
	//header("Location: $current_url");
	
	}
	//每页数
	
if(isset($_GET[s_text])){
	if($_GET[s_type]=='PRODUCT_NAME'){
		$is_type_null=$_GET[s_text]==null?'':' and `name` LIKE \'%'.$_GET[s_text].'%\'';
		}elseif($_GET[s_type]=='PRODUCT_MODEL'){
			$model = $db->select("SELECT a.sid FROM `".tablepre."stock_attachment` a left join `".tablepre."stock` b on a.sid = b.sid where b.onsale = 1 and b.delete = 0 and a.type = 'model' and a.value1 like '%".$_GET[s_text]."%'");
			if(count($model)>0){
			for($i=0;$i<count($model);$i++){
				$modelarr .= $model[$i][0].',';
				}
				$modelarr_e = array_filter(explode(',',$modelarr));
				$modelarr_i = implode(',',$modelarr_e);
				$is_type_null=$_GET[s_text]==null?'':' and `sid` in ('.$modelarr_i.')';
			}else{exit('<script>alert("'.NOTE_ISNOT_CATALOGUE_PRODUCTS.'"); window.history.back(-1);</script>');}
			}
			elseif($_GET[s_type]=='PRODUCT_BATCH_NO'){
				$batch = $db->select("SELECT a.sid FROM `".tablepre."stock_item` a left join `".tablepre."stock` b on a.sid = b.sid where b.onsale = 1 and b.delete = 0 and a.batch like '%".$_GET[s_text]."%'");
				if(count($batch)>0){
					for($i=0;$i<count($batch);$i++){
						$batcharr .= $batch[$i][0].',';
						}
						$batcharr_e = array_filter(explode(',',$batcharr));
						$batcharr_i = implode(',',$batcharr_e);
						$is_type_null=$_GET[s_text]==null?'':' and `sid` in ('.$batcharr_i.')';
					
					}else{exit('<script>alert("'.NOTE_ISNOT_CATALOGUE_PRODUCTS.'"); window.history.back(-1);</script>');}
				}elseif($_GET[s_type]=='PRODUCT_PRICE'){
					
					$check_array_price =strpos($_GET[s_price], '>');
					if($check_array_price!== false){
						$s_price=substr($_GET[s_price],1);
						$item = $db->select("SELECT sid FROM `".tablepre."stock_item` where `price` > '".$s_price."'");
						for($i=0;$i<count($item);$i++){
							$price_arr .= $item[$i][0].',';
							}
							$price_arr_e = array_filter(explode(',',$price_arr));
							$price_arr_i = implode(',',$price_arr_e);
							$is_type_null=$_GET[s_price]==null?'':' and `sid` in ('.$price_arr_i.')';
						
						}else{
							//print_r($_GET);
							//echo $leveltype[0][2]*0.01;
							$e_s_price = explode('-',$_GET[s_price]);
							$e_s_price_1=round($e_s_price[0],2);
							$e_s_price_2=round($e_s_price[1],2);
							$item = $db->select("SELECT sid,price FROM `".tablepre."stock_item` where `price` * '".$leveltype[0][2]*0.01."' BETWEEN '".$e_s_price_1."' and '".$e_s_price_2."'");
							for($i=0;$i<count($item);$i++){
								//echo $item[$i][1].'-';
								$price_arr .= $item[$i][0].',';
								}
								$price_arr_e = array_filter(explode(',',$price_arr));
								$price_arr_i = implode(',',$price_arr_e);
								$is_type_null=$_GET[s_price]==null?'':' and `sid` in ('.$price_arr_i.')';
							}
					
					}
					//按价钱
	
	}else{
if($_GET[tid]<>null){
	
	function arr_father($con){
	global $db; global $tablepre;
	$arr_type_list = $db->select("SELECT tid,father FROM `".tablepre."stock_type` where father = $con");
	for($i=0;$i<count($arr_type_list);$i++){
		$arr .= $arr_type_list[$i][0].',';
		//arr_father($con=$arr_type_list[$i][0]);
		}
		return $arr;
	}
	
$arr_type_list = $db->select("SELECT tid,father FROM `".tablepre."stock_type` where father = ".$_GET[tid]."");
if($arr_type_list[0][0]==null){
	$tid_array=$_GET[tid];
	}else{
for($i=0;$i<count($arr_type_list);$i++){
	
	$tid_array .= arr_father($arr_type_list[$i][0]).','.$arr_type_list[$i][0].',';
	
	}
$tid_array1 = array_filter(explode(',',$tid_array));
$tid_array = implode(',',$tid_array1);
	}
$arr_type_list = $db->select("SELECT tid,father FROM `".tablepre."stock_type` where father in (".$tid_array.")");

if($arr_type_list[0][0]==null){$tid_array4=$tid_array; }else{
for($i=0;$i<count($arr_type_list);$i++){
	$tid_array2 .= $arr_type_list[$i][0].',';
	}
	$tid_array2 .= $tid_array;
$tid_array3 = array_filter(explode(',',$tid_array2));
$tid_array5 = array_merge($tid_array1,$tid_array3);
$tid_array4 = implode(',',$tid_array3);
}
}

///
$e_tid = explode(',',$tid_array4);
//print_r($e_tid);
foreach($e_tid as $v){
$to_catalogue = $db->select("SELECT products_id FROM `".tablepre."to_categories` WHERE `categories_id` = '".$v."'");
//print_r($to_catalogue);
if(count($to_catalogue)>0){
	for($i=0;$i<count($to_catalogue);$i++){
		//echo $to_catalogue[$i][0];
			$onsale = $db->select("SELECT onsale FROM `we_stock` WHERE `onsale` = '1' and sid = '".$to_catalogue[$i][0]."'");
	//print_r($onsale);
		if($onsale[0][0]<>null){
		$arr.=$to_catalogue[$i][0].',';
		}
		
		}
}
		$pid_array=array_filter(explode(',',$arr));
		if(count($pid_array)>0){
			$pid_array2 = 'OR sid in('.implode(',',$pid_array).')';
			}
		
	}

////to catalogue

$is_type_null=$_GET[tid]==null?'':'and tid in ('.$tid_array4.')';
}

  $newSql="select count(sid) as count from `".tablepre."stock` where onsale = 1 ".$is_type_null." ".$pid_array2."";//获取表中的总记录数 
@$resultCount=mysql_query($newSql,$dbh);// or die("执行SQL语句失败!");//返回结果集 
@$reArray=mysql_fetch_assoc($resultCount);//从结果集中取出一行,作为相关数组处理 
$amount=$reArray['count'];//得到总记录数 
if($_COOKIE[perpage]==null or $_COOKIE[perpage]=='0'){
	$pageSize=16;
	}else{
		$pageSize=$_COOKIE[perpage];
		}
$pageamount=floor($pageamount=$amount/$pageSize); //总页数
//////////////////////分类页数
$offset=isset($_GET[page])?($_GET[page]-0)*$pageSize:0;
include('include/bpage.php'); 
//分页
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><? echo SITE_HEADER; ?></title>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> 
<link href="css/admin.css" rel="stylesheet" type="text/css" />
	<!-- jquery packed -->
	<script type="text/javascript" src="js/jquery-1.4.4.js"></script>
    <script type="text/javascript" src="js/dtree.js"></script>
    <script type="text/javascript" src="js/ajaxcart.js"></script>
	<!-- tableRowCheckboxToggle -->
    <script type="text/javascript"> 
    function fade(b) 
{ 
    $('<div id="add" class="quick-alert2"><? include_once('include/cart_check.php'); ?></div>').insertAfter($("#example"+b)).animate({opacity: 1.0}, 3000).fadeOut("slow",function(){  
   $(this).remove(); 
}); 
} 
</script> 
<script>
function close_facebox(){
	alert('<? echo CART_ADDED; ?>');
 $('#facebox').fadeOut(function() {
      $('#facebox .content').removeClass().addClass('content')
    })
	}
</script>
<style type="text/css">
/**************************/
#page{}
#page ul{
	margin: 0px;
	padding: 0px;
}
#page .page2{
	margin: 3px;
	padding: 3px;
	float: left;
	border: 1px solid #FFF;
	list-style-type: none;
}
#page ol{
	margin: 3px;
	float: left;
	border: 1px solid #C03434;
	list-style-type: none;
	padding-top: 3px;
	padding-right: 5px;
	padding-bottom: 3px;
	padding-left: 5px;
}
#page dl{
	margin: 3px;
	float: left;
	border: 1px solid #C03434;
	list-style-type: none;
	padding-top: 3px;
	padding-right: 5px;
	padding-bottom: 3px;
	padding-left: 5px;
	background-color: #FFDFDF;
	font-weight: bold;
}
/*****************************/
.latestWeb ul,li{ list-style:none;}
.latestWeb a{ color:#333; text-decoration:none;}
.latestWeb a:hover{ color:#ff722d; text-decoration:none;}
.latestWeb img{ border:0; float:left;}
.latestWeb a img,a:hover img{ border:0;}
.latestWeb{ width:770px;}
.latestWeb ul{ overflow:hidden; _height:1%; margin:0px; padding:0px;}
.latestWeb li{ float:left; border:1px solid #EBEAEA; width:360px; padding:3px; margin:14px 14px 0 0; height:120px; }
.latestWeb .cn{
	float:left; margin:0 0 0 5px; width:250px;}
.latestWeb .cn h2{
	font-size:12px; font-weight: bold; margin:0px; padding:0px;}
.latestWeb .cn h3{
	font-size:12px; font-weight:normal; margin:0px; padding:0px;}
.latestWeb .cn input{
	margin:0px;
	padding:3px 3px 3px 22px;
	border:#666 solid 1px;
	height:21px;
	background:#FFF;
	background-image: url(images/pcde_307.png);
	background-repeat: no-repeat;
	background-position: 3px 1px;
	float:left;
}
</style>
<link href="js/facefiles/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="js/facefiles/facebox.js" type="text/javascript"></script>

<script type="text/javascript">
    jQuery(document).ready(function($) {
      $('a[rel*=facebox]').facebox() 
    })
</script>
</head>
<body>
<div id="wrap">
<div id="main" class="clearfix">
<div id="side">
  </div>
  <div id="head"><? include_once('user_head.php');?></div><table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="top"><div id="left"> <?
  include_once('user_menu.php');
  ?></div></td>
    <td valign="top">
    
  <div class="latestWeb">
<ul>
<?
echo nav();
?>
<div style="float:right; margin:10px 0 0 0; width:420px; text-align:right;"><div style="text-align:left; float:left; margin:7px 0 0 0;"><? echo $amount.' '.CATALOGUE_ITEMS.' | '.CATALOGUE_ITEM_PER_PAGE;?>: 
<? eval("\$perpagesty = ".parameters(33));
foreach($perpagesty as $v){
	echo '<a href="?perpage='.$v.'">'.$v.'</a> | ';
	}
?>
</div>
<?
foreach($_GET as $k => $v){
	if($k<>'page'){
		$url .= $k.'='.$v.'&';
		
		}		
	}
?><script>
function page(pageamount){
	var pageamount = parseInt(pageamount)+1;
	var value=document.getElementById("page").value;
	var gotopage;
	if(value == null){
		gotopage = 0;
		}else if(value>=pageamount){
			gotopage = pageamount;
			
			}else{
				gotopage = value;		
				}
	document.getElementById("page").value = gotopage;
	}
function page_go_to(){
	var value=parseInt(document.getElementById("page").value)-1;
	location='?<? echo $url; ?>&page='+value;
	}
</script>

<? echo CATALOGUE_GO_TO_PAGE;?> &nbsp;<input type="text" value="<? echo $_GET[page]+1;?>" size="1" id="page" onkeyup="page('<? echo $pageamount; ?>')" onkeydown="if(event.keyCode==13)event.keyCode=9" onKeyPress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false" /> &nbsp; <input type="button" value="<? echo CATALOGUE_GO; ?>" onclick="page_go_to()" /></div>
<?
echo $pages;
?>
<form name="myform" method="post" style="margin:0; padding:0;"  >
<?
if(!isset($_GET[s_text])){$is_type_null=$_GET[tid]==null?'':'and tid in ('.$tid_array4.')';}
$arr_list = $db->select("SELECT * FROM `".tablepre."stock` where `delete` = '0' and onsale = 1 ".$is_type_null." ".$pid_array2." order by sid desc limit $offset,$pageSize");
for($i=0;$i<count($arr_list);$i++) {
	$pic = attachment($con0=1,$con1=$arr_list[$i][0],$con2=pic);
	$loadpic = explode("/", $pic );
	//picture
	$name=$arr_list[$i][1]==null?PRODUCT_UNTITLED:$arr_list[$i][1];
 echo '<li>
<div><a href="user_stock_view.php?sid='.$arr_list[$i][0].'&'.rand(0,15).'" rel="facebox"><img height=100 width=100 src="'.pic_return(attachment($con0=1,$con1=$arr_list[$i][0],$con2=pic)).'" onMouseOver="toolTip(\'<img src='.pic_return(attachment($con0=1,$con1=$arr_list[$i][0],$con2=pic)).'>\')" onMouseOut="toolTip()"></a><div class="cn">
<h2 style="height:29px;">'.$name.'</h2>
'.CATALOGUE_AVERAGE_PRICE.'：'.average_price($arr_list[$i][0],$checkmember[0][2],$unit[0][0]).'<br />
'.$leveltype[0][1].' ('.(100-$leveltype[0][2]).'%'.MEMBER_OFF.')<br />
'.PRODUCT_MODEL.'：'.attachment($con0=1,$con1=$arr_list[$i][0],$con2=model).'<br />
'.PRODUCT_CURRENT_STOCK.'：'.current_item_ctock($arr_list[$i][0]).' '.parameters(24).' | '.PRODUCT_MINIMUN_ORDER.minimun($arr_list[$i][0]).' '.parameters(24).'<br />
<input type="button" value="'.CART_ADD_TO_CART.'" onclick="checkusername(\''.$arr_list[$i][0].'\'),fade(\''.$arr_list[$i][0].'\')" /><div id="example'.$arr_list[$i][0].'"></div>
</div><div id="clear"></div></div>
</li>';

}
?>
</form>
</ul>

<? echo $pages;?>
</div></td>
  </tr>
</table>
 
  
  </div>
  </div>

<div id="footer">
<?
include_once('fooder.php');
?>
</div>
</BODY>
</HTML>
<script language="javascript">
    function getViewportHeight() {
        if (window.innerHeight != window.undefined) return window.innerHeight;
        if (document.compatMode == 'CSS1Compat') return document.documentElement.clientHeight;
        if (document.body) return document.body.clientHeight;

        return window.undefined;
    }
    function getViewportWidth() {
        if (window.innerWidth != window.undefined) return window.innerWidth;
        if (document.compatMode == 'CSS1Compat') return document.documentElement.clientWidth;
        if (document.body) return document.body.clientWidth;

        return window.undefined;
    }

    /**
    * Gets the real scroll top
    */
    function getScrollTop() {
        if (self.pageYOffset) // all except Explorer
        {
            return self.pageYOffset;
        }
        else if (document.documentElement && document.documentElement.scrollTop)
        // Explorer 6 Strict
        {
            return document.documentElement.scrollTop;
        }
        else if (document.body) // all other Explorers
        {
            return document.body.scrollTop;
        }
    }
    function getScrollLeft() {
        if (self.pageXOffset) // all except Explorer
        {
            return self.pageXOffset;
        }
        else if (document.documentElement && document.documentElement.scrollLeft)
        // Explorer 6 Strict
        {
            return document.documentElement.scrollLeft;
        }
        else if (document.body) // all other Explorers
        {
            return document.body.scrollLeft;
        }
    }
    //--初始化变量--
    var rT = true; //允许图像过渡
    var bT = true; //允许图像淡入淡出
    var tw = 150; //提示框宽度
    var endaction = false; //结束动画
    var ns4 = document.layers;
    var ns6 = document.getElementById && !document.all;
    var ie4 = document.all;
    offsetX = 10;
    offsetY = 20;
    var toolTipSTYLE = "";
    function initToolTips() {
        tempDiv = document.createElement("div");
        tempDiv.id = "toolTipLayer";
        tempDiv.style.position = "absolute";
        tempDiv.style.display = "none";
        document.body.appendChild(tempDiv);
        if (ns4 || ns6 || ie4) {
            if (ns4) toolTipSTYLE = document.toolTipLayer;
            else if (ns6) toolTipSTYLE = document.getElementById("toolTipLayer").style;
            else if (ie4) toolTipSTYLE = document.all.toolTipLayer.style;
            if (ns4) document.captureEvents(Event.MOUSEMOVE);
            else {
                toolTipSTYLE.visibility = "visible";
                toolTipSTYLE.display = "none";
            }
            document.onmousemove = moveToMouseLoc;
        }
    }
    function toolTip(msg, fg, bg) {
        try {
            if (toolTip.arguments.length < 1) // hide
            {
                if (ns4) {
                    toolTipSTYLE.visibility = "hidden";
                }
                else {
                    //--图象过渡，淡出处理--
                    if (!endaction) { toolTipSTYLE.display = "none"; }
                    if (rT) document.all("msg1").filters[1].Apply();
                    if (bT) document.all("msg1").filters[2].Apply();
                    document.all("msg1").filters[0].opacity = 0;
                    if (rT) document.all("msg1").filters[1].Play();
                    if (bT) document.all("msg1").filters[2].Play();
                    if (rT) {
                        if (document.all("msg1").filters[1].status == 1 || document.all("msg1").filters[1].status == 0) {
                            toolTipSTYLE.display = "none";
                        }
                    }
                    if (bT) {
                        if (document.all("msg1").filters[2].status == 1 || document.all("msg1").filters[2].status == 0) {
                            toolTipSTYLE.display = "none";
                        }
                    }
                    if (!rT && !bT) toolTipSTYLE.display = "none";
                    //----------------------
                }
            }
            else // show
            {
                if (!fg) fg = "#777777";
                if (!bg) bg = "#eeeeee";
                var content =
		'<table id="msg1" name="msg1" border="0" cellspacing="0" cellpadding="1" bgcolor="' + fg + '" class="trans_msg"><td>' +
		'<table border="1" cellspacing="2" cellpadding="3" bgcolor="' + bg +
		'"><td><font face="Arial" color="' + fg +
		'" size="-2">' + msg +
		'</font></td></table></td></table>';

                if (ns4) {
                    toolTipSTYLE.document.write(content);
                    toolTipSTYLE.document.close();
                    toolTipSTYLE.visibility = "visible";
                }
                if (ns6) {
                    document.getElementById("toolTipLayer").innerHTML = content;
                    toolTipSTYLE.display = 'block'
                }
                if (ie4) {
                    document.all("toolTipLayer").innerHTML = content;
                    toolTipSTYLE.display = 'block'
                    //--图象过渡，淡入处理--
                    var cssopaction = document.all("msg1").filters[0].opacity
                    document.all("msg1").filters[0].opacity = 0;
                    if (rT) document.all("msg1").filters[1].Apply();
                    if (bT) document.all("msg1").filters[2].Apply();
                    document.all("msg1").filters[0].opacity = cssopaction;
                    if (rT) document.all("msg1").filters[1].Play();
                    if (bT) document.all("msg1").filters[2].Play();
                    //----------------------
                }
            }
        } catch (e) { }
    }
    function moveToMouseLoc(e) {
        var scrollTop = getScrollTop();
        var scrollLeft = getScrollLeft();
        if (ns4 || ns6) {
            x = e.pageX + scrollLeft;
            y = e.pageY - scrollTop;
        }
        else {
            x = event.clientX + scrollLeft;
            y = event.clientY;
        }

        if (x - scrollLeft > getViewportWidth() / 2) {
            x = x - document.getElementById("toolTipLayer").offsetWidth - 2 * offsetX;
        }

        if ((y + document.getElementById("toolTipLayer").offsetHeight + offsetY) > getViewportHeight()) {
            y = getViewportHeight() - document.getElementById("toolTipLayer").offsetHeight - offsetY;
        }
        toolTipSTYLE.left = (x + offsetX) + 'px';
        toolTipSTYLE.top = (y + offsetY + scrollTop) + 'px';
        return true;
    }
    initToolTips();

</script>